<template>
	<view class="updatePage">
		<view class="title">
			设置{{title}}
		</view>
		<view class="inputBorder">
			<!-- <input v-model="value" :type="type" :disabled="disabled" placeholder="请在此进行输入"> -->
			<uni-easyinput style="height: 40px;line-height: 40px;" trim="all" :inputBorder="false" v-model="value"
				:type="type" :disabled="disabled" placeholder="请在此进行输入"></uni-easyinput>
		</view>
		<button v-if="disabled" type="default"  style="margin-top: 30px;"
			@click="getPhoneNumber">获取手机号</button>
		<button v-if="title!='签收授权码'" class="finsh" type="primary" :disabled="!value" @click="finsh">完成</button>
		<button v-else class="finsh" type="primary" @click="finsh">完成</button>
	</view>
</template>

<script>
	import WXBizDataCrypt from '@/utils/WXBizDataCrypt.js'
	const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
	export default {
		data() {
			return {
				title: '',
				type: 'text',
				value: '',
				disabled: false,
				phoneCode: '',
				openId: '',
				unionid: '',
				session_key: '',
				token: ''
			}
		},
		onLoad(options) {
			this.title = options.title == 'undefined' ? '' : options.title
			this.type = options.type == 'undefined' ? '' : options.type
			this.value = options.value == 'undefined' ? '' : options.value
			if (this.title == '手机号') {
				this.disabled = true
				this.getPhone()
			}
			let userInfo = uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo') : {}
		},
		methods: {
			// 获取手机号权限
			getPhone() {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						this.phoneCode = res.code
						console.log(this.phoneCode);
						this.$http('post', '/Base_Manage/Base_User/GetWxData?JsCode=' + this.phoneCode +
							'&type=hzq').then(
							result => {
								if (result.Success) {
									let data = JSON.parse(JSON.parse(result.Data).data)
									this.openId = data.openid //openid 用户唯一标识  
									this.unionid = data.unionid //unionid 开放平台唯一标识
									this.session_key = data.session_key //session_key  会话密钥  
								}
							})
					}
				})
			},
			// 获取手机号
			getPhoneNumber(e) {
				let phone =uni.getStorageSync('userInfo').phone
				if(phone){
					this.value=phone
					uni.showToast({
						title:'手机号已获取',
						icon:"none"
					})
				}
				setTimeout(function(){
					uni.navigateBack()
				},1500)
				
				// if (e.detail.errMsg.indexOf('fail') != -1) return
				
				// let pc = new WXBizDataCrypt('wxa24101ce1f340e47', this.session_key);
				// let data = pc.decryptData(e.detail.encryptedData, e.detail.iv);
				// this.value = data.phoneNumber
			},
			// 完成
			async finsh() {
				if (this.title == '身份证号' && !reg.test(this.value)) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的身份证号'
					})
					return
				}
				let userInfo = uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo') : {}
				this.back(userInfo)
			},
			// back
			back(userInfo) {
				if (this.title == '姓名') userInfo.name = this.value
				else if (this.title == '手机号') userInfo.phone = this.value
				else if (this.title == '身份证号') userInfo.idCard = this.value
				else userInfo.smscode = this.value
				uni.setStorageSync('userInfo', userInfo)
				uni.navigateBack()
			},
		},
	}
</script>

<style lang="scss" scoped>
	::v-deep.uni-easyinput__content-input {
		height: 40px;
	}

	.updatePage {
		box-sizing: border-box;
		height: 100vh;
		padding: 30px;

		.title {
			font-weight: 500;
			font-size: 24px;
			text-align: center;
			margin-bottom: 10vh;
		}

		.inputBorder {
			height: 40px;
			border-top: 1px solid #ccc;
			border-bottom: 1px solid #ccc;
		}

		.finsh {
			position: absolute;
			bottom: 100px;
			left: 30px;
			width: calc(100% - 60px);
		}
	}
</style>